const { logger } = require("./log");
const crypto = require("./crypto");
const requestLevel = {
  success: 1,
  warn: 2,
  fail: 0,
  error: -1,
  loginTimeOut: -99,
};
const { success, warn, fail, error, loginTimeOut } = requestLevel;
exports.message = ({ res, type = success, msg = "加载成功", data = {} }) => {
  let backMsg = {
    result: type,
    state: 200,
    msg,
    data,
  };
  switch (type) {
    case success:
      logger.info(JSON.stringify(backMsg));
      break;
    case warn:
      logger.warn(JSON.stringify(backMsg));
      break;
    case fail:
      logger.fatal(JSON.stringify(backMsg));
      break;
    case error:
    case loginTimeOut:
      logger.error(JSON.stringify(backMsg));
      break;
    default:
      logger.log(JSON.stringify(backMsg));
      break;
  }
  res.sendMsg = res.send.bind(res, { params: crypto.setCrypto(backMsg) });
};
exports.requestLevel = requestLevel;
